package com.dexilaonian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.adpter.ServiceAdapter;
import com.dexilaonian.bean.ServiceBean;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.JsonUtil;
import com.dexilaonian.utils.Okhttp;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class ServiceActivity extends BaseActivity {
    List<ServiceBean> service_list = new ArrayList<>();
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.base_choose)
    TextView baseChoose;
    @Bind(R.id.servic_listview)
    ListView servicListview;
    @Bind(R.id.base_topText)
    TextView baseTopText;
    @Bind(R.id.btn_service_shenqing)
    Button btnServiceShenqing;
    @Bind(R.id.service_title)
    TextView serviceTitle;
    private ServiceAdapter adapter;
    private Intent intent;
    private String service_list_url = Constant.base_url + "/customer/list.do";
    private String address;
    private String realName;
    private String province;
    private String city;
    private String tel;
    private int gender;
    private int customer_id;
    private ArrayList<ServiceBean> serviceBean;
    private String content_time;
    private String mNotice;
    private String mContent_method;
    private int price;
    private String serviceuserid;
    private String projectid;
    private Bundle extras;

    @Override
    public int getLayoutID() {
        return R.layout.activity_service;
    }

    @Override
    public void initView() {
    }

    @Override
    protected void initData() {

    }

    private void initGetService() {
        Map map = new HashMap();
        Okhttp.postString(true, service_list_url, map, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {

            }
            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject object = new JSONObject(response);
                    int status = Integer.parseInt(object.optString("status"));
                    if (status == 34 || status == 16) {
                        startActivity(new Intent(ServiceActivity.this, LoginActivity.class));
                        return;
                    }
                    if (status == 0) {
                        String data = object.optString("data");
                        serviceBean = JsonUtil.json2beans(data, ServiceBean.class);
                        for (int i = 0; i < serviceBean.size(); i++) {
                            int customer_id = serviceBean.get(i).getCustomer_id();
                            address = serviceBean.get(i).getAddress();
                            realName = serviceBean.get(i).getRealName();
                            province = serviceBean.get(i).getProvince();
                            city = serviceBean.get(i).getCity();
                            tel = serviceBean.get(i).getTel();
                            gender = serviceBean.get(i).getGender();
                        }
                        service_list.addAll(serviceBean);
                        adapter = new ServiceAdapter(ServiceActivity.this, service_list);
                        servicListview.setAdapter(adapter);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initGetService();
        intent = getIntent();
        if (intent != null) {
            extras = intent.getExtras();
            if (extras != null) {
                String confirm = extras.getString("CONFIRM");
                if (!TextUtils.isEmpty(confirm)) {
                    content_time = extras.getString("mContent_time");
                    mNotice = extras.getString("mNotice");
                    mContent_method = extras.getString("mContent_method");
                    serviceuserid = extras.getString("serviceuserid");
                    projectid = extras.getString("projectid");
                    price = extras.getInt("PRICE");
                    btnServiceShenqing.setVisibility(View.GONE);
                    servicListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        private String originalPrice;
                        private String name;
                        private String contentTime;
                        private String introduce;

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            adapter.setSelectedPosition(position);
                            adapter.notifyDataSetInvalidated();
                            customer_id = serviceBean.get(position).getCustomer_id();
                            String realName = serviceBean.get(position).getRealName();
                            String address = serviceBean.get(position).getAddress();
                            int customer_id = serviceBean.get(position).getCustomer_id();
                            String city = serviceBean.get(position).getCity();
                            String province = serviceBean.get(position).getProvince();
                            String realAddress = province + city + address;
                            showToast(realAddress + customer_id);
                            String confirm00 = extras.getString("CONFIRM00");
                            if (TextUtils.isEmpty(confirm00)) {
                                intent = new Intent(ServiceActivity.this, ConfirmOrderActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putInt("PRICE", price);
                                bundle.putInt("customer_id", customer_id);
                                intent.putExtra("serviceuserid", serviceuserid);
                                intent.putExtras(bundle);
                                intent.putExtra("mContent_time", content_time);
                                intent.putExtra("mNotice", mNotice);
                                intent.putExtra("mContent_method", mContent_method);
                                intent.putExtra("realAddress", realAddress);
                                intent.putExtra("realName", realName);
                                intent.putExtra("projectid", projectid);
                                intent.putExtra("CONFIRM2", "CONFIRM2");
                                startActivity(intent);
                            } else {
                                intent = new Intent(ServiceActivity.this, AgingConfiActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putInt("PRICE", price);
                                bundle.putInt("customer_id", customer_id);
                                String projectid22 = extras.getString("projectid");
                                introduce = extras.getString("INTRODUCE");
                                contentTime = extras.getString("ContentTime");
                                name = extras.getString("NAME");
                                originalPrice = extras.getString("originalPrice");
                                intent.putExtra("serviceuserid", serviceuserid);
                                intent.putExtras(bundle);
                                intent.putExtra("mContent_time", content_time);
                                intent.putExtra("mNotice", mNotice);
                                intent.putExtra("mContent_method", mContent_method);
                                intent.putExtra("realAddress", realAddress);
                                intent.putExtra("realName", realName);
                                intent.putExtra("projectid", projectid22);
                                intent.putExtra("CONFIRM2", "CONFIRM2");
                                intent.putExtra("INTRODUCE", introduce);
                                intent.putExtra("originalPrice", originalPrice);
                                intent.putExtra("ContentTime", contentTime);
                                intent.putExtra("NAME", name);
                                startActivity(intent);
                            }
                        }
                    });
                } else {
                    servicListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            adapter.setSelectedPosition(position);
                            adapter.notifyDataSetInvalidated();
                            customer_id = serviceBean.get(position).getCustomer_id();
                        }
                    });
                }
            }
        }
        serviceTitle.setText("服务人员");
        baseChoose.setVisibility(View.VISIBLE);
        baseChoose.setText("添加");
        baseChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ServiceActivity.this, EditActivity.class);
                startActivity(intent);
            }
        });
    }

    @OnClick({R.id.back, R.id.base_topText, R.id.btn_service_shenqing})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
            case R.id.base_topText:
                finish();
                break;
            case R.id.btn_service_shenqing:
                initTijiao();
                break;
        }
    }

    private void initTijiao() {
        showProgressBar(true);
        Map<String, String> map = new HashMap();
        map.put("customerId", customer_id + "");
        String service_shengqing_url = Constant.base_url + "/vipCard/applyVIPCard.do";
        Okhttp.postString(true, service_shengqing_url, map, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {
                showProgressBar(false);
            }

            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 34 || status == 16) {
                        startActivity(new Intent(ServiceActivity.this, LoginActivity.class));
                        return;
                    }
                    if (status == 36) {
                        showToast("该用户已申请孝心卡");
                        return;
                    }
                    if (status == 0) {
                        Intent intent = new Intent(ServiceActivity.this, ApplyingAcitivity.class);
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {
                showProgressBar(false);
            }
        });
    }
}
